package submarines.console;

import java.io.*;
import java.util.Random;

import submarines.logic.PlayerBase;

/** Eldar **/
public class gameUI {

	// loop over all players.
	
		// if player CPU -> call autoCPUPlay() in logic
		// else if player PLAYER -> showMenu() in console
			// "Select (still in GAME) player to attack": P1 P2 ...
				// A. ??? SELECT CELLS
				// B. ??? CANCEL CELLS
				// C. Shoot at target -> igrid.bomb('A', 3)
					// bomb should update the specified grid, per the attacker vs. attacked players.
			
				// read the updated grid from logic & display the attacked grid on screen. 
	
				// if you are the only one standing.. you WIN !  -> get from logic
	
	private static int gridSize = 10; 
	
	public void mainUI() {  // INCOMPLETE
		
		PlayerBase[] playersList = null;

		try {
		
			// Get list of all the players from logic
			;
			
			// Loop over the users
			for (int i=0; i<playersList.length; i++) {
				
				if (playersList[i].isInGame()) {
					
					// Human
					if (playersList[i].getType()) {
						this.displayUIMenu(playersList[i].getName());
					}
					// CPU
					else {
						this.selectCellForCPU(playersList[i].getName());
					}
				
				}
			}
			
		}
		catch (IOException e) {
		
			// Do something...
			;
		}
	}
	
	private void displayUIMenu(String currentUser) throws IOException {  // INCOMPLETE
		
		String playerToAttack = "";
		String selectedCellToAttack = "";
		int actionOption = 0;
		
		System.out.println("User " + currentUser + ", please select your target" );
		
		// get list of all active users (still in game)
		// and display them, except for currentUser! 1. 2. 3....
		;
		
		// select user to attack
		BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));
		playerToAttack = stdin.readLine();

		/////////////
		
		// Get and Display current grid
		this.displayUIGridByPlayer(currentUser, playerToAttack);
		
		// Action Options to select
		System.out.println("1. Mark Cell.");
		System.out.println("2. Cancel Cells Marking.");
		System.out.println("3. Bombs Away, Now!");
		
		actionOption = stdin.read();
		
		/////////////////////////
		
		// Select Cells
		if (actionOption == 1) {
			markCell(true);
		}
		// Cancel Cells Selection
		else if (actionOption == 2) {
			markCell(false);
		}
		// Bombs Away, Now!
		else if (actionOption == 3) {
			System.out.println("Please select a cell to attack (e.g: A5):");
			
			// Get input HERE
			selectedCellToAttack = stdin.readLine();
			
			// Call logic here to execute the attack
			;
		}
		
	}
	
	private String selectCellForCPU(String cpuPlayerId) {  // INCOMPLETE
		
		String randomCellStr = null;
		boolean notOriginalEnough = false;
		String attackedPlayerId = null;
		PlayerBase[] playersList = null;
		boolean goodToAttack = false;
		int randomId = -1;
		int horizontalChar = 0;
		int verticalNum = -1;
		
		Random randomGenerator = new Random();
		
		// Get list of all players
		;
		
		// Select a random player to attack
		do {
			
			randomId = randomGenerator.nextInt(playersList.length);
			
			if (playersList[randomId].isInGame() == true) {
				if (cpuPlayerId != playersList[randomId].getName()) {
					attackedPlayerId = playersList[randomId].getName();
					goodToAttack = true;
				}
			}
			
		} while (goodToAttack == false);

		// get the relevant grid for this two players: attacker vs. attacked.
		this.displayUIGridByPlayer(cpuPlayerId, attackedPlayerId);
		
		while (!notOriginalEnough) {

			// select random cell here
			horizontalChar = 97 + randomGenerator.nextInt(gridSize) + 1; // ascii value of a letter
			verticalNum = randomGenerator.nextInt(gridSize) + 1;
		
			// make sure the random cell wasn't used before by this player to the attacked player!! (get grid again... sigh)
			;
		
		}

		return(String.format("%c%d", horizontalChar, verticalNum));
	}
	
	private void markCell(boolean action) throws IOException {  // INCOMPLETE
		
		BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));
		
		System.out.println("Please select a cell: ");
		//actionOption = stdin.read();
		String cellMark = stdin.readLine();
		
		// Mark a cell
		if (action == true) {
			; // Call the logic function with 'cellMark'
		}
		// Unmark a cell
		else {
			; // Call the logic function with 'cellMark'
		}
		
	}
	
	private void displayUIGridByPlayer(String userAttacker, String userAttacked) { // INCOMPLETE
		
		String gridStr = null;  // examples of the expected grid string: A9XF4*B0?D0*
		char[][] myGrid = new char [gridSize][gridSize]; 
		
		// X  ->  hit
		// *  ->  miss
		// -  ->  empty
		// ?  ->  marked option
		
		// Create an empty Grid
		for (int iV=0; iV<gridSize; iV++) {
			for (int iH=0; iH<gridSize; iH++) {
			
				myGrid[iV][iH] = '-';
			}
		}
		
		// Get specified grid from Logic
		;
		
		// Fill the empty grid with gridStr
		for (int i=0; i<gridStr.length(); i+=3) {
			
			int letter = (int) gridStr.charAt(i) - 65;  // Dec. ascii for UpperCase A
			int num	= (int) gridStr.charAt(i+1) - 48;   // Dec. ascii for Zero
			
			myGrid[num][letter] = gridStr.charAt(i+2);  // Place the right sign in the display grid
		}
		
		// Display the grid to the player
		System.out.println(".:: " + userAttacker + "  vs.  " + userAttacked + " ::.");
		System.out.println("  A B C D E F G H I J");
		for (int iH=0; iH<gridSize; iH++) {
			
			System.out.print(iH + " ");
			for (int iV=0; iV<gridSize; iV++) {
			
				System.out.print(myGrid[iH][iV]);
			}
			
			System.out.println("");
		}
		
	}
	
	
} // Class ends


